package primephysique;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import primephysique.ConnectDB;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/*
 * SearchStaff.java
 *
 * Created on Apr 11, 2012, 8:26:26 PM
 */
/**
 *
 * @author Kyle
 */
public class SearchStaff extends javax.swing.JPanel {

    /**
     * Creates new form SearchStaff
     */
    public SearchStaff() {
        initComponents();
        viewResult();
    }

    private void viewResult() {
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String ts = txtSearchStaff.getText().toString();
            // get range of infomation
            String query = "SELECT Account,FullName,DateOfbirth,Gender,Address,Phone,Salary FROM Staff WHERE FullName like '%" + ts + "%'";
            rs = st.executeQuery(query);
            rsmd = rs.getMetaData();
            Vector<String> columnName = new Vector<String>();
            Vector<String> columnData;
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                columnName.add(rsmd.getColumnName(i + 1));
            }
            // set table column's Name
            model = new DefaultTableModel();
            int cc = rsmd.getColumnCount();
            for (int i = 0; i < cc; i++) {
                model.addColumn(columnName.get(i));
            }

            while (rs.next()) {
                columnData = new Vector<String>();
                for (int i = 0; i < cc; i++) {
                    columnData.add(rs.getString(i + 1));
                    //     System.out.println(rs.getString(i + 1));
                }
                model.addRow(columnData);
                //         columnData.clear();
            }
            tbSearch.setModel(model);
            scpSearch.setViewportView(tbSearch);
            con.close();


        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbSearch = new javax.swing.JLabel();
        txtSearchStaff = new javax.swing.JTextField();
        btnSearchStaff = new javax.swing.JButton();
        scpSearch = new javax.swing.JScrollPane();
        tbSearch = new javax.swing.JTable();

        lbSearch.setText("Search");

        btnSearchStaff.setText("Search");
        btnSearchStaff.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchStaffActionPerformed(evt);
            }
        });

        tbSearch.setModel(model);
        scpSearch.setViewportView(tbSearch);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(lbSearch)
                .addGap(40, 40, 40)
                .addComponent(txtSearchStaff, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(74, 74, 74)
                .addComponent(btnSearchStaff))
            .addComponent(scpSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbSearch)
                    .addComponent(txtSearchStaff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchStaff))
                .addGap(43, 43, 43)
                .addComponent(scpSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

        private void btnSearchStaffActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchStaffActionPerformed
            viewResult();
    }//GEN-LAST:event_btnSearchStaffActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearchStaff;
    private javax.swing.JLabel lbSearch;
    private javax.swing.JScrollPane scpSearch;
    private javax.swing.JTable tbSearch;
    private javax.swing.JTextField txtSearchStaff;
    // End of variables declaration//GEN-END:variables
    private ConnectDB cdb = null;
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private ResultSetMetaData rsmd;
    private DefaultTableModel model = new DefaultTableModel();
}
